package cc.fs.zhihuapp;

import android.content.Context;

import org.apache.http.HttpException;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * Created by fostion on 2015/9/6.
 */
public class AppException extends Exception implements Thread.UncaughtExceptionHandler{
    /**
     * 定义异常类型
     */
    public static byte TYPE_NETWORK = 1;
    public static byte TYPE_SOCKET = 2;
    public static byte TYPE_HTTP_CODE = 3;
    public static byte TYPE_HTTP_ERROR = 4;
    public static byte TYPE_XML = 5;
    public static byte TYPE_IO = 6;
    public static byte TYPE_RUN = 7;
    public static byte TYPE_JSON = 8;
    public static byte TYPE_FILENOTFOUND = 9;

    /**
     * 获取APP异常崩溃处理对象
     *
     * @param context *
     * @return
     */
    public static AppException getAppExceptionHandler(Context context) {
        return new AppException(context.getApplicationContext());
    }

    public static AppException http(Exception e) {
        return new AppException(TYPE_HTTP_ERROR, 0, e);
    }

    public static AppException http(int code) {
        return new AppException(TYPE_HTTP_CODE, code, null);
    }

    public static AppException run(Exception e) {
        return new AppException(TYPE_RUN, 0, e);
    }

    public static AppException socket(Exception e) {
        return new AppException(TYPE_SOCKET, 0, e);
    }

    public static AppException json(Exception e) {
        return new AppException(TYPE_JSON, 0, e);
    }

    public static AppException xml(Exception e) {
        return new AppException(TYPE_XML, 0, e);
    }

    public static AppException file(Exception e) {
        return new AppException(TYPE_FILENOTFOUND, 0, e);
    }

    // io异常
    public static AppException io(Exception e) {
        return io(e, 0);
    }

    public static AppException io(Exception e, int code) {
        if (e instanceof UnknownHostException || e instanceof ConnectException) {
            return new AppException(TYPE_NETWORK, code, e);
        } else if (e instanceof IOException) {
            return new AppException(TYPE_IO, code, e);
        }
        return run(e);
    }

    // 网络请求异常
    public static AppException network(Exception e) {
        if (e instanceof UnknownHostException || e instanceof ConnectException) {
            return new AppException(TYPE_NETWORK, 0, e);
        } else if (e instanceof HttpException) {
            return http(e);
        } else if (e instanceof SocketException) {
            return socket(e);
        }
        return http(e);
    }

    private byte type;// 异常的类型
    // 异常的状态码，这里一般是网络请求的状态码
    private int code;

    /**
     * 系统默认的UncaughtException处理类
     */
    private App mContext;

    public AppException(Context context) {
        this.mContext = (App) context;
    }

    public AppException(Byte type, int code, Exception exp) {
        super(exp);
        this.type = type;
        this.code = code;
    }


    public void uncaughtException(Thread thread, Throwable ex) {
        if (handleException(ex)) {
            ex.printStackTrace();
            System.exit(0);
        }
    }

    /**
     * 自定义异常处理:收集错误信息&发送错误报告
     *
     * @return true:处理了该异常信息;否则返回false
     */
    private boolean handleException(Throwable ex) {
        return true;
    }
}
